from flask import Flask,request
from email_utils import send_code
app = Flask(__name__)


code = 0
# 获取注册验证码
@app.route("/get_code")
def get_code():
    global code
    email = request.args.get("email")
    code = send_code(email)
    print(code)
    return str(code)

username = ""
password = ""
# 用户注册
@app.route("/reg")
def reg():
    global username
    global password
    username = request.args.get("username")
    print(username)
    password = request.args.get("password")
    print(password)
    user_code = request.args.get("code")
    print(user_code)
    print(code)
    if str(code) == str(user_code):
        return "注册成功"
    else:
        return "注册失败"

# 用户登录
@app.route("/login")
def login():
    username_param = request.args.get("username")
    password_param = request.args.get("password")
    if username == username_param and password_param == password:
        return "登录成功"
    else:
        return "用户名或者是密码错误"


@app.route("/post1", methods=["POST"])
def my_post1():
    return "这是我的第一个post请求"


# form格式的参数 post请求
@app.route("/post2",methods=["post"])
def my_post2():
    # 如何form格式的参数呢？
    username = request.form["username"]
    print(username)
    password = request.form["password"]
    print(password)
    return "你输入的用户名是{},你输入的密码是{}!!!".format(username,password)

# json格式的post请求
@app.route("/post3",methods=["post"])
def my_post3():
    request_data = request.get_json()
    print(request_data)
    print(type(request_data))
    print(request_data["username"])
    print(request_data["password"])
    return "你输入的用户名是{},你输入的密码是{}!!!".format(request_data["username"],
                                                           request_data["password"])

if __name__ == "__main__":
    app.run()
